package com.one.ui;

import com.one.component.BackGroundPanel;
import com.one.component.BookManagePanel;
import com.one.util.CheckCodeFromMysql;
import com.one.util.ScreenUtils;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

public class addBookPage {
    JFrame jFrame = new JFrame("添加图书");
    BackGroundPanel backGroundPanel = new BackGroundPanel(new ImageIcon("images\\bookAdd.png").getImage());

    final int WIDTH = 720;
    final int HEIGHT = 480;

    JLabel bookId = new JLabel("图 书 编 号:");
    JLabel bookName = new JLabel("书           名:");
    JLabel bookAuthor = new JLabel("作           者:");
    JLabel bookPublisher = new JLabel("出   版   社:");
    JLabel bookValue = new JLabel("单           价:");
    JLabel bookFreeNumber = new JLabel("剩   余   量:");
    JLabel bookTotal = new JLabel("总           量:");

    JTextField bookIdText = new JTextField(15);
    JTextField bookNameText = new JTextField(15);
    JTextField bookAuthorText = new JTextField(15);
    JTextField bookPublisherText = new JTextField(15);
    JTextField bookValueText = new JTextField(15);
    JTextField bookFreeNumberText = new JTextField(15);
    JTextField bookTotalText = new JTextField(15);

    JButton ok = new JButton("确定");
    JButton back = new JButton("返回主页面");


    public void init() {
        //组装视图
        jFrame.setBounds((ScreenUtils.getScreenWidth()-WIDTH) / 2,
                (ScreenUtils.getScreenHeight()-HEIGHT) / 2, WIDTH, HEIGHT);
        jFrame.setIconImage(new ImageIcon("images//logo.png").getImage());

        Box hBox1 = Box.createHorizontalBox();
        Box hBox2 = Box.createHorizontalBox();
        Box hBox3 = Box.createHorizontalBox();
        Box hBox4 = Box.createHorizontalBox();
        Box hBox5 = Box.createHorizontalBox();
        Box hBox6 = Box.createHorizontalBox();
        Box hBox7 = Box.createHorizontalBox();
        Box hBox8 = Box.createHorizontalBox();

        hBox1.add(bookId);
        hBox1.add(Box.createHorizontalStrut(30));
        hBox1.add(bookIdText);

        hBox2.add(bookName);
        hBox2.add(Box.createHorizontalStrut(30));
        hBox2.add(bookNameText);

        hBox3.add(bookAuthor);
        hBox3.add(Box.createHorizontalStrut(30));
        hBox3.add(bookAuthorText);

        hBox4.add(bookPublisher);
        hBox4.add(Box.createHorizontalStrut(33));
        hBox4.add(bookPublisherText);

        hBox5.add(bookValue);
        hBox5.add(Box.createHorizontalStrut(30));
        hBox5.add(bookValueText);

        hBox6.add(bookFreeNumber);
        hBox6.add(Box.createHorizontalStrut(33));
        hBox6.add(bookFreeNumberText);

        hBox7.add(bookTotal);
        hBox7.add(Box.createHorizontalStrut(30));
        hBox7.add(bookTotalText);

        hBox8.add(Box.createHorizontalStrut(40));
        hBox8.add(ok);
        hBox8.add(Box.createHorizontalStrut(200));
        hBox8.add(back);

        Box content = Box.createVerticalBox();
        content.add(Box.createVerticalStrut(40));
        content.add(hBox1);
        content.add(Box.createVerticalStrut(20));
        content.add(hBox2);
        content.add(Box.createVerticalStrut(20));
        content.add(hBox3);
        content.add(Box.createVerticalStrut(20));
        content.add(hBox4);
        content.add(Box.createVerticalStrut(20));
        content.add(hBox5);
        content.add(Box.createVerticalStrut(20));
        content.add(hBox6);
        content.add(Box.createVerticalStrut(20));
        content.add(hBox7);
        content.add(Box.createVerticalStrut(50));
        content.add(hBox8);

        backGroundPanel.add(content);
        jFrame.add(backGroundPanel);

        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jFrame.setVisible(true);

        //事件处理
        ok.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int id = Integer.parseInt(bookIdText.getText().trim());
                String name = bookNameText.getText().trim();
                String author = bookAuthorText.getText().trim();
                String publisher = bookPublisherText.getText().trim();
                double value = Double.parseDouble(bookValueText.getText().trim());
                int freeNum = Integer.parseInt(bookFreeNumberText.getText().trim());
                int total = Integer.parseInt(bookTotalText.getText().trim());

                if (CheckCodeFromMysql.isBookNumberExits(id)) {
                    JOptionPane.showMessageDialog(jFrame, "此图书编号已经存在，请换一个编号，谢谢！");
                } else {
                    try {
                        BookManagePanel.addBook(id, name, author, publisher, value, freeNum, total);
                        JOptionPane.showMessageDialog(jFrame, "图书增加成功！即将返回主页面。");
                        jFrame.dispose();
                        new ManageInterface().init();
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        });

        back.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jFrame.dispose();
                new ManageInterface().init();
            }
        });
    }

    public static void main(String[] args) {
        new addBookPage().init();
    }

}
